<%@page import="com.tubeonfire.model.AdvertisingModel"%>
<%@page import="com.tubeonfire.entity.Advertising"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.tubeonfire.model.admin.SiteConfigModel"%>
<%@page import="com.tubeonfire.entity.SiteConfig"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.tubeonfire.model.PlaylistModel"%>
<%@page import="com.tubeonfire.entity.Playlist"%>
<%@page import="java.util.*"%>
<%
	request.setCharacterEncoding("UTF-8");
	PlaylistModel model = (PlaylistModel) request.getAttribute("model");
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	SiteConfig siteConfig = SiteConfigModel.get();
	String logoUrl = siteConfig.getLogoKey();
	if (!logoUrl.equals("/images/logo.png")) {
		logoUrl = "http://" + request.getServerName() + "/image?key="
				+ logoUrl + "&size=250";
	}	
	String pageUrl = (String) request.getAttribute("url");
	String pageTitle = "List Playlist - Page " + model.getPage();
	String pageName = siteConfig.getSiteName();
	String pageDescription = "List Playlist. "
			+ siteConfig.getSiteDescription();
	String pageKeyword = siteConfig.getSiteKeyword();
	String pageImage = logoUrl;
	String facebookAdminId = siteConfig.getFacebookAdminId();
	String pageLogoUrl = logoUrl;
	String pageLogoSlogan = siteConfig.getLogoSlogan();
	String analyticCode = siteConfig.getAnalyticCode();
	int place = 7;
	int position = 1;
%>
<jsp:include page="/element/header.jsp">	
	<jsp:param name="pageUrl" value="<%= pageUrl %>" />
	<jsp:param name="pageTitle" value="<%=pageTitle %>" />
	<jsp:param name="pageName" value="<%=pageName %>" />
	<jsp:param name="pageImages" value="<%=pageImage %>" />
	<jsp:param name="pageKeyword" value="<%=pageKeyword %>" />
	<jsp:param name="pageDescription" value="<%=pageDescription %>" />
	<jsp:param name="facebookAdminId" value="<%=facebookAdminId %>" />
	<jsp:param name="pageLogoUrl" value="<%=pageLogoUrl %>" />
	<jsp:param name="pageLogoSlogan" value="<%=pageLogoSlogan %>" />
</jsp:include>

<div id="content">
	<div id="content_inner">
		<div id="leftsidecontent">
			<section>
				<div class="block">
					<div class="search-result">
						<h3 class="button blue">
							<a href="/list-playlist">List Playlist</a>
						</h3>
						<%
							Advertising ads = AdvertisingModel.getByPlaceAndPosition(place,
									position);
						%>
						<%
							if (ads != null&&ads.getId().length()>0) {
						%>
						<br />
						<div class="advWrap adv-468x60 clearafter" style="border: none;">
							<div>
								<%
									if (ads.getType() == 1) {
								%>
								<%=ads.getContent()%>
								<%
									} else {
											String imgUrl = ads.getContent();
											imgUrl = "/image?key=" + imgUrl + "&size=468";
								%>
								<a title="<%=ads.getTitle()%>" href="<%=ads.getLink()%>"> <img
									alt="<%=ads.getTitle()%>" src="<%=imgUrl%>">
								</a>
								<%
									}
								%>
							</div>
						</div>
						<%
							} else if (user != null && userService.isUserAdmin()) {
						%>
						<br />
						<div class="advWrap adv-468x60 clearafter">
							<div>
								<a
									href="/admin/advertising/add?place=<%=place%>&position=<%=position%>"
									title="Add Advertising">Add Advertising Here (468 x 60)</a>
							</div>
						</div>
						<%
							}
						%>
						<div class="video-listing clearafter">
							<%
								if (model != null && model.getListResult().size() > 0) {
									for (Playlist obj : model.getListResult()) {
										String imgUrl = obj.getImageKey();
										if (!imgUrl.contains("http://")) {
											imgUrl = "/image?key=" + imgUrl + "&size=250";
										}
							%>
							<div class="video-items" id="<%=obj.getAlias()%>">
								<div class="video-items_inner">
									<a class="link-image-scale"
										href="/playlist/<%=obj.getId()%>/<%=obj.getAlias()%>"
										title="<%=obj.getTitle()%>"> <img class="imageToScale"
										src="<%=imgUrl%>" alt="<%=obj.getTitle()%>"
										title="<%=obj.getDescription()%>" />
									</a>
									<h3 class="video-title">
										<a href="/playlist/<%=obj.getAlias()%>"
											title="<%=obj.getTitle()%>"><%=obj.getSubTitle()%></a>
										<%
											if (user != null && userService.isUserAdmin()) {
										%>
										<a target="blank"
											href="/admin/playlist/edit?id=<%=obj.getId()%>"
											style="font-weight: normal; color: green;">(Edit)</a>
										<%
											}
										%>
									</h3>
								</div>
							</div>
							<%
								}
								}
							%>
						</div>

						<%
							position = 2;
							ads = AdvertisingModel.getByPlaceAndPosition(place, position);
						%>
						<%
							if (ads != null&&ads.getId().length()>0) {
						%>						
						<div class="advWrap adv-468x60 clearafter" style="border: none;">
							<div>
								<%
									if (ads.getType() == 1) {
								%>
								<%=ads.getContent()%>
								<%
									} else {
											String imgUrl = ads.getContent();
											imgUrl = "/image?key=" + imgUrl + "&size=468";
								%>
								<a title="<%=ads.getTitle()%>" href="<%=ads.getLink()%>"> <img
									alt="<%=ads.getTitle()%>" src="<%=imgUrl%>">
								</a>
								<%
									}
								%>
							</div>
						</div>
						<br />
						<%
							} else if (user != null && userService.isUserAdmin()) {
						%>						
						<div class="advWrap adv-468x60 clearafter">
							<div>
								<a
									href="/admin/advertising/add?place=<%=place%>&position=<%=position%>"
									title="Add Advertising">Add Advertising Here (468 x 60)</a>
							</div>
						</div>
						<br />
						<%
							}
						%>
					</div>
				</div>
			</section>
			<%
				if (model.getTotalPage() > 1) {
			%>
			<ul class="paginate">
				<%
					if (model.getPage() > 1) {
				%>
				<li><a class="first" href="<%=pageUrl%>?page=1">First</a></li>
				<li><a class="first"
					href="<%=pageUrl%>?page=<%=model.getPage() - 1%>">Previous</a></li>
				<%
					}
						int startPage = 1;
						int endPage = 7;
						int currentPage = model.getPage();
						int totalPage = model.getTotalPage();
						if (currentPage <= 3) {
							startPage = 1;
							if (totalPage >= 7) {
								endPage = 7;
							} else {
								endPage = totalPage;
							}
						} else {
							startPage = currentPage - 3;
							if ((totalPage - currentPage) > 3) {
								endPage = currentPage + 3;
							} else {
								endPage = totalPage;
								startPage = totalPage - 6;
								if (startPage <= 0) {
									startPage = 1;
								}
							}
						}
						for (int i = startPage; i <= endPage; i++) {
							if (model.getPage() == i) {
				%>
				<li><a class="first"
					href="<%=pageUrl%>?page=<%=model.getPage()%>"><%=i%></a></li>
				<%
					} else {
				%>
				<li><a class="first" href="<%=pageUrl%>?page=<%=i%>"><%=i%></a></li>
				<%
					}
						}
						if (model.getTotalPage() > model.getPage()) {
				%>
				<li><a class="first"
					href="<%=pageUrl%>?page=<%=model.getPage() + 1%>">Next</a></li>
				<li><a class="first"
					href="<%=pageUrl%>?page=<%=model.getTotalPage()%>">Last</a></li>
				<%
					}
				%>
			</ul>
			<%
				}
			%>
		</div>
		<jsp:include page="/element/right-content-all-playlist.jsp">
			<jsp:param value="<%=(user!=null&&userService.isUserAdmin())?'1':'0' %>" name="isAdmin"/>
		</jsp:include>
		<div class="clearbreak"></div>
	</div>
</div>
<jsp:include page="/element/footer.jsp">
	<jsp:param name="analyticCode" value="<%= analyticCode %>" />
</jsp:include>